/*
 * @Date: 2023-03-27 14:29:00
 * @LastEditors: Chuying
 * @LastEditTime: 2023-04-01 17:38:56
 * @Description:
 */
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  BadRequestException,
  UseGuards,
  Req,
} from '@nestjs/common';
import { Request } from 'express';
import { LocalAuthGuard } from './guards/local-auth.guard';

import { AuthService } from './auth.service';
import { UserService } from './../user/user.service';
import { CreateAuthDto } from './dto/create-auth.dto';
import { UpdateAuthDto } from './dto/update-auth.dto';

import { UserStatusDTO } from './../user/dto/user-status.dto';
import { NoAuth } from 'src/common/decorator/no-auth.decorator';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

declare module 'express' {
  interface Request {
    user: UserStatusDTO;
  }
}
@ApiTags('账号管理')
@Controller('auth')
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private readonly userService: UserService,
  ) {}

  // 登录接口
  @ApiOperation({
    description:'登入',
    summary:'登入'
  })
  @NoAuth()
  @UseGuards(LocalAuthGuard)
  @Post('login')
  login(@Req() req: Request) {
    try {
      return this.authService.login(req.user);
    } catch (error) {
      throw new BadRequestException(error.message);
    }
  }

  //   查询个人信息;
  @ApiOperation({
    description:'获取当前用户信息',
    summary:'获取当前用户信息'
  })
  @Get('userInfo')
  me(@Req() req: Request) {
    // console.log(req.user, '===========');
    return this.userService.findUserInfo(req.user.id);
  }

  @Post()
  create(@Body() createAuthDto: CreateAuthDto) {
    return this.authService.create(createAuthDto);
  }

  @Get()
  findAll() {
    return this.authService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.authService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateAuthDto: UpdateAuthDto) {
    return this.authService.update(+id, updateAuthDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.authService.remove(+id);
  }
}
